package cn.itcast.flink.window;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * * 滚动时间窗口案例演示：实时交通卡口流量统计，每隔5秒统计最近5秒钟各个卡口流量
 *
 * @author lilulu
 */
public class WindowReduceDemo {
    public static void main(String[] args) throws Exception {
        // 1. 执行环境-env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 2. 数据源-source
        DataStreamSource<String> source = env.socketTextStream("node1", 9999);
        // 3. 数据转换-transformation
        SingleOutputStreamOperator<Tuple2<String, Integer>> reduceStream = source.filter(line -> line.trim().split(",").length == 2)
                .map(new MapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String value) throws Exception {
                        System.out.println("每条卡口流量数据: " + value);
                        String[] split = value.split(",");
                        return Tuple2.of(split[0], Integer.parseInt(split[1]));
                    }
                }).keyBy(tuple2 -> tuple2.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> reduce(Tuple2<String, Integer> before, Tuple2<String, Integer> current) throws Exception {
                        /*todo: 调用reduce算子时，要求聚合返回值数据类型，与数据类型相同
                            tmp:
                                对窗口中数据聚合时，存储聚合中间结果变量，类型与窗口中数据类型一致
                                todo: 如果数据为窗口中第1条数据，直接赋值给tmp，不会调用reduce方法增量聚合
                                    (a, 10)
                            item:
                                窗口中每条数据, todo: 从窗口中第2条数据开始赋值
                                    (a, 20)*/
                        Integer historyValue = before.f1;
                        Integer currentValue = current.f1;
                        int newValue = historyValue + currentValue;
                        System.out.println(
                                "以前聚合结果: tmp = " + before + ", 当前数据: item = " + current + ", 聚合后值: latest = " + newValue
                        );
                        return Tuple2.of(before.f0, newValue);
                    }
                });
        // 4. 数据终端-sink
        reduceStream.printToErr();
        // 5. 触发执行-execute
        env.execute("WindowReduceDemo");
    }
}